﻿package src 
{
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import lib.component.event.componentEvent;
  import lib.component.resouceMonitor.resouceMonitorView;
	import lib.meatClass.meatMenu;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;
	import src.controler.product_builder;
	import src.event.menuEvent;
  import src.event.proOperateEvent;
	import src.model.data_model;
  import src.plugin.buildin.playerControlPanelView;
	import src.view.flat_face_view;
	import src.view.ui.topBarView;
  import src.view.ui.windowView;
	
	/**
	 * 3d产品展示，使用pv3d作为3d框架
	 * 目的是使用少量的工作完成较好的效果
	 * @author	MeatHill
	 * @version	0.1(2009-05-12)
	 */
	public class mainShopWindow extends Sprite
	{
		private var _viewport:Viewport3D;
		private var _scene:Scene3D;
		private var _camera:Camera3D;
		private var _render:BasicRenderEngine;
		private var _data:data_model;
		private var _mouse_pos:Point;
		private var _zoom_max:uint = 60;
		private var _zoom_min:uint = 20;
		private var _product:product_builder;
    private var _monitor:resouceMonitorView;
    private var _alt_mc:windowView;
		private var _top_bar_mc:topBarView;
    private var _bg_mc:Sprite;
		private var _is_debug_bl:Boolean = true;
		
		public function mainShopWindow() 
		{
			if (stage) {
				init();
			} else {
				addEventListener(Event.ADDED_TO_STAGE, init);
			}
		}
		
		/*******************
		 * functions
		 * ****************/
		private function init(evt:Event = null):void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			contextMenu = new meatMenu(true, null, null, '0.1').contextMenu;
			if (loaderInfo.parameters['debug']) {
				_is_debug_bl = true;
			}
			
			_data = new data_model();
			_data.addEventListener(Event.COMPLETE, data_load_complete_handler);
			_data.addEventListener(IOErrorEvent.IO_ERROR, data_load_failed_handler);
			_data.load('n78.xml');
			
			init3D();
			display_init();
		}
		private function display_init():void {
      // 资源监视器
			if (_is_debug_bl) { 
				_monitor = new resouceMonitorView();
				addChild(_monitor);
			}
      
      // 底部mc
      _bg_mc = Sprite(getChildAt(0));
      
      // alt内容显示窗口
      _alt_mc = new windowView(false);
			
			// 顶部通栏
			_top_bar_mc = new topBarView();
			_top_bar_mc.addEventListener(menuEvent.SHOW_DETAIL, menu_handler);
			addChild(_top_bar_mc);
			
			_bg_mc.addEventListener(MouseEvent.MOUSE_DOWN, mouse_down_handler);
			_bg_mc.addEventListener(MouseEvent.MOUSE_WHEEL, mouse_wheel_handler);
			stage.focus = _bg_mc;
		}
		private function init3D():void {
			//设置渲染引擎
			_render = new BasicRenderEngine ();
			
			//创建viewport
			_viewport = new Viewport3D(0, 0, true, true);
      _viewport.interactive = true;
			addChild(_viewport);
			
			_scene = new Scene3D();
			
			_camera = new Camera3D();
			//_camera.zoom = 20;
		}
		private function data_load_complete_handler(evt:Event):void {
			_product = new product_builder(_data.part_data);
      _product.addEventListener(proOperateEvent.SHOW_ALT, show_alt_msg);
      _product.addEventListener(proOperateEvent.REMOVE_ALT, remove_alt_msg);
      _scene.addChild(_product, 'product');
			
			_top_bar_mc.add_pro_title(_data.pro_name, _data.menu_data);
      
      refresh_scene();
		}
		private function data_load_failed_handler(evt:IOErrorEvent):void {
			trace("data load failed..");
		}
		private function mouse_down_handler(evt:MouseEvent):void {
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouse_move_handler);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouse_release_handler);
			
			_mouse_pos = new Point(evt.stageX, evt.stageY);
		}
		private function mouse_move_handler(evt:MouseEvent):void {
      _product.rotationY += Math.round((_mouse_pos.x - evt.stageX) / 2);
      _product.rotationX += Math.round((_mouse_pos.y - evt.stageY) / 2);
			
			_mouse_pos.x = evt.stageX;
			_mouse_pos.y = evt.stageY;
			
			refresh_scene();
		}
		private function mouse_release_handler(evt:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouse_move_handler);
			stage.removeEventListener(MouseEvent.MOUSE_UP, mouse_release_handler);
		}
		private function mouse_wheel_handler(evt:MouseEvent):void {
			if ((evt.delta < 0 && _camera.zoom > _zoom_min + evt.delta) || (evt.delta > 0 && _camera.zoom < _zoom_max - evt.delta)) {
				_camera.zoom += evt.delta;
				refresh_scene();
			}
		}
		private function refresh_scene():void {
			_render.renderScene(_scene, _camera, _viewport);
		}
    private function show_alt_msg(evt:proOperateEvent):void {
      _alt_mc.x = stage.mouseX + 8;
      _alt_mc.y = stage.mouseY;
      _alt_mc.label = evt.title;
      _alt_mc.content = evt.content;
      addChild(_alt_mc);
    }
    private function remove_alt_msg(evt:proOperateEvent):void {
      if (_alt_mc && contains(_alt_mc)) {
        removeChild(_alt_mc);
      }
    }
		private function menu_handler(evt:menuEvent):void {
			switch(evt.command) {
				case 'external':
          if (getChildByName(evt.name)) {
            var _window:windowView = windowView(getChildByName(evt.name));
            _window.visible = true;
          } else {
            var _window:windowView = new windowView();
            _window.name = evt.name;
            _window.label = evt.name;
            _window.x = 100;
            _window.y = 100;
            _window.w = evt.param.width;
            _window.h = evt.param.height;
            _window.load(evt.param.src);
            _window.addEventListener(componentEvent.CLOSE, remove_window);
            addChild(_window);
          }
					break;
          
        case 'movie':
          if (getChildByName(evt.name)) {
            var _window:windowView = windowView(getChildByName(evt.name));
            _window.visible = true;
          } else {
            // 查找播放器的位置
            var _tar:XML = _data.get_target_player(evt.param.target);
            //_product.play_movie(_tar, evt.param.src);
            var _playerControler:playerControlPanelView = new playerControlPanelView(evt.param.src, evt.param.width, evt.param.height);
            var _window:windowView = new windowView();
            _window.name = evt.name;
            _window.label = evt.name;
            _window.x = 90;
            _window.y = 110;
            _window.loadUI(_playerControler);
            _window.addEventListener(componentEvent.CLOSE, remove_window);
            addChild(_window);            
            
            _playerControler.addVideoInside();
            _playerControler.playMovie();
          }
          
          break;
					
				default:
					break;
			}
		}
		private function remove_window(evt:componentEvent):void {
			windowView(evt.currentTarget).visible = false;
		}
	}
}